package com.wondream.myframework.app.request;

import com.alibaba.druid.support.json.JSONUtils;
import com.wondream.myframework.app.common.util.HttpUtil;
import com.wondream.myframework.app.common.util.MD5Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseRequest {

    public final String merchantKey = "C45FBF52D47A91C3EAA47B2A0D5D8D57";

//    public String url = "http://localhost:8080/bill-merc";
    public String url = "http://appbillmerc.52bill.com/bill-merc";
    public String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzZXNzaW9uS2V5IjpudWxsLCJvcGVuSWQiOiJvUmNBazVTR2xJbWVfNHpCNWhrdXpyUVFzNGs4Iiwic3ViIjoiMTAwMDY0IiwiaWF0IjoxNjIwMjc1ODA5LCJleHAiOjE2MjAzNjIyMDl9.-Dt5YAeodi6L4Z_1FG_i4_gfiXID0qq90BTjQlACPv7sgUGxp1OpgfdkBtKdiBY8xKoHUDZw568IYWvbE7rDhA";

    public final Log logger = LogFactory.getLog(BaseRequest.class);

    public final Logger logger4j = LoggerFactory.getLogger(BaseRequest.class);

    public Map<String, String> dataMap = new HashMap<String, String>();

    public final String md5key = "BD1FAD3DF7DE2AC6EE9F0C7C8CEFC3F6";
    public final String billMercMd5key = "3FBABDD80AC438613E4324D8CA447B66";


    public final String busiOnlinePayMd5key="3FBABDD80CC438613E4324A8CA447B66";

    public String apiSignature = "";
    public String response = "";
    public Map responseDataMap = null;

    public abstract void excute();
    public abstract void initSendData();

    public String getApiSignature(){
        apiSignature = MD5Util.apiSign(dataMap, billMercMd5key, logger);
        logger.info("");
        logger.info("==========当前api请求签名： "+apiSignature);
        logger.info("");
        return apiSignature;
    }

    public void addCommonSendData(){
        //////////////////增加默认数据/////////////////
        dataMap.put("charSet", "02");
        dataMap.put("signType", "MD5");
        dataMap.put("version", "1.0");
        dataMap.put("usrId", "100064");
//        dataMap.put("shopId", "100");
        //////////////////增加默认数据/////////////////
    }

    public String sendApiRequest(){
        url = url+"/api";
        this.addCommonSendData();
        this.initSendData();
        dataMap.put("signature", this.getApiSignature());
        response = HttpUtil.httpClientPost(url, dataMap, token);
        logger.info("");
        logger.info("============================ 请求返回结果：");
        logger.info(response);
        logger.info("");
        if(response!=null&&response!=""){
            try {
                responseDataMap = (Map) JSONUtils.parse(this.response);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return response;
    }

    public void sendTranReqeust(){
        String className = this.getClass().getName();
        className = className.replace(".", "-");
        String[] nameArray = className.split("-");
//        for (String var1:
//             nameArray) {
//            logger.info(var1);
//        }
        int length = nameArray.length;
        String serName = nameArray[length-2];
        String code = nameArray[length-1];
        code = code.substring(0,length);
        url = url+"/"+serName+"/"+code+".tran";
        logger.info("请求地址："+url);

        response = HttpUtil.sendPost(url, dataMap);
        logger.info(response);
    };

}
